package com.prtek.reminder.model;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import com.prtek.reminder.bean.DaoSession;
import com.prtek.reminder.bean.RemindUser;
import com.prtek.reminder.bean.RemindUserDao;
import com.prtek.reminder.bean.User;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;

public class UserManager {

  private static final String CONTENT_RELATION = "content://com.prtek.iis.relation";
  private static final String CONTENT_USER = "content://com.prtek.iis.user";
  public static final Uri URI_USER = Uri.parse(CONTENT_USER + File.separator + "user");
  public static final Uri URI_RELATION = Uri.parse(CONTENT_RELATION + File.separator + "relation");
  private static Context mContext;



  public static ArrayList<User> getUserList() {
    ArrayList<User> userList = new ArrayList<User>();
    Cursor cursor = query(URI_USER, null, null, null, null);
    if (null != cursor) {
      while (cursor.moveToNext()) {
        User user = parseCursor(cursor);
        userList.add(user);
      }
      cursor.close();
    }
    return userList;
  }

  public static User getUserById(long id) {

    Cursor cursor = query(URI_USER, null, "id =?", new String[] { String.valueOf(id) }, null);
    if (null == cursor) {
      return null;
    }

    User user = cursor.moveToNext() ? parseCursor(cursor) : null;
    cursor.close();
    return user;
  }

  public static HashSet<String> queryRelation() {
    HashSet<String> hs = new HashSet<String>();
    Cursor cursor = query(URI_RELATION, null, null, null, null);
    if (cursor != null) {
      while (cursor.moveToNext()) {
        String relation = cursor.getString(cursor.getColumnIndex("relation"));
        if (!TextUtils.isEmpty(relation)) {
          hs.add(relation);
        }
      }
      cursor.close();
    }
    return hs;
  }

  public static long queryMinorId(long majorId, String relation) {

    Cursor cursor =
        query(URI_RELATION, new String[] { "minoruserid" }, "majoruserid =? and relation =?",
            new String[] { String.valueOf(majorId), relation }, null);

    if (cursor == null) {
      return -1;
    }

    long id = cursor.moveToNext() ? cursor.getLong(cursor.getColumnIndex("minoruserid")) : -1;
    cursor.close();
    return id;
  }

  private static Cursor query(Uri uri, String[] projection, String selection, String[] Args,
      String Order) {
    return mContext.getContentResolver().query(uri, projection, selection, Args, Order);
  }

  public static void init(Context context) {
    mContext = context;
    final ArrayList<User> userList = getUserList();
    final DaoSession daoSession = ReminderDaoImpl.getInstance().getDaoSession();
    final RemindUserDao userDao = daoSession.getRemindUserDao();
    for (User user : userList) {
      RemindUser remindUser = new RemindUser(user.getUserId());
      userDao.insertOrReplace(remindUser);
    }
  }

  private static User parseCursor(Cursor cursor) {
    User user = new User();
    user.setUserId(cursor.getLong(cursor.getColumnIndex("id")));
    user.setName(cursor.getString(cursor.getColumnIndex("name")));
    user.setAge(cursor.getInt(cursor.getColumnIndex("age")));
    user.setSex(cursor.getString(cursor.getColumnIndex("sex")));
    user.setNickName(cursor.getString(cursor.getColumnIndex("nickname")));
    user.setImage(cursor.getBlob(cursor.getColumnIndex("image")));
    return user;
  }

}
